﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class OrderProductRepository : RepositoryBase<OrderProduct>
    {
        /// <summary>
        /// 根据orderId查看还没有（原料成本核算和工艺核算）的订单个数
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int GetCheckOrderProductByOrderId(Guid orderId)
        {
            var result = from op in dbcontext.OrderProducts
                         join tech in dbcontext.Technologies
                             on op.Id equals tech.OrderProductId
                         join ori in dbcontext.OriginalCosts
                             on tech.Id equals ori.TechnologytId
                         where op.OrderId == orderId && (tech.Status == -1 || ori.Status == -1)
                         select new ProductTechnologyView
                         {
                             TechnologyStatus = tech.Status,
                             OriginalCostStatus = ori.Status
                         };
            return result.ToList().Count();
        }

        /// <summary>
        /// 根据orderId查看还没有（工艺核算）的订单个数
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int GetCheckTOrderProductByOrderId(Guid orderId)
        {
            var result = from op in dbcontext.OrderProducts
                         join tech in dbcontext.Technologies
                         on op.Id equals tech.OrderProductId
                         where op.OrderId == orderId && (tech.Status == -1)
                         select new ProductTechnologyView
                         {
                             TechnologyStatus = tech.Status,
                         };
            return result.ToList().Count();
        }

        /// <summary>
        /// 根据orderId查看还没有（加工生产）的订单个数（worksheet）
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int GetCheckWorksheetByOrderId(Guid orderId)
        {
            var result = from op in dbcontext.OrderProducts
                         join work in dbcontext.Worksheets
                         on op.WorksheetId equals work.Id
                         where op.OrderId == orderId && (work.Status == -1)
                         select new ProductTechnologyView
                         {
                             TechnologyStatus = work.Status,
                         };
            return result.ToList().Count();
        }

        /// <summary>
        /// 根据orderid获取关系表对应的工艺（Technologies）和产品表（Products）信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public IQueryable<ProductTechnologyView> GetOrderProductsByOrderId(Guid orderId)
        {
            var result = from op in dbcontext.OrderProducts
                         join tech in dbcontext.Technologies
                             on op.Id equals tech.OrderProductId
                         join pro in dbcontext.Products
                             on op.ProductId equals pro.Id
                         where op.OrderId == orderId// && tech.Status != -1 && pro.Status == 1
                         select new ProductTechnologyView
                         {
                             TechnologyId = tech.Id,
                             ProductId = pro.Id,
                             ProductName = pro.Name,
                             CreateTime = tech.CreateTime,
                             Amount = op.Amount,
                             Price = op.Price
                         };
            return result;
        }
        /// <summary>
        /// 根据orderid获取关系表对应的工艺（Technologies）和产品表（Products）信息
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <returns></returns>
        public ProductTechnologyView GetOrderProductsByOrderProductId(Guid orderProductId)
        {
            var result = from op in dbcontext.OrderProducts
                         join tech in dbcontext.Technologies
                         on op.Id equals tech.OrderProductId
                         join pro in dbcontext.Products
                         on op.ProductId equals pro.Id
                         where op.Id == orderProductId// && tech.Status != -1 && pro.Status == 1
                         select new ProductTechnologyView
                         {
                             TechnologyId = tech.Id,
                             ProductId = pro.Id,
                             OrderId = op.OrderId.Value,
                             ProductName = pro.Name,
                             CreateTime = tech.CreateTime,
                             Amount = op.Amount,
                             Price = op.Price,
                             OrderProductId = op.Id,
                             DistributeType = tech.DistributeType,
                         };
            return result.FirstOrDefault();
        }

        /// <summary>
        /// 获取订单对应产品信息
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public IQueryable<ProductView> GetProductListByOrder(Guid orderID)
        {
            var result = from op in dbcontext.OrderProducts
                         join pro in dbcontext.Products on op.ProductId equals pro.Id
                         where op.OrderId == orderID
                         select new ProductView
                         {
                             Id = op.Id,
                             Name = pro.Name,
                             Number = pro.Number,
                             //Type = pro.Type,
                             Price = op.Price,

                             PriceTime = op.PriceTime,
                             CostPrice=op.CostPrice,
                             FinalPrice = op.FinalPrice,
                             Amount = op.Amount,
                             SendAmount=op.SendAmount,
                             ProductID = pro.Id,
                             
                         };
            return result;
        }
    }
}
